home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14872 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: admaix.sunydutchess.edu!ub!newserve!rebecca!rpi!not-for-mail
  2. From: floydb1@lib104.its.rpi.edu (Barry B Floyd)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Leap year
  5. Date: 1 Apr 1996 11:11:09 -0500
  6. Organization: Rensselaer Polytechnic Institute, Troy, NY.
  7. Message-ID: <4jov6t$737@lib104.its.rpi.edu>
  8. References: <3135A7F2.2120@hiwaay.net> <4hbiln$899@sam.inforamp.net> <4hesba$ph@clarknet.clark.net> <3146D0D8.16BA@msn.com> <315b3f2b.912029@NEWS.CLOUD9.NET> <Dp1LD9.Mt@mv.mv.com>
  9. NNTP-Posting-Host: lib104.its.rpi.edu
  10. X-newsreader: xrn 7.04-beta-11
  11.  
  12.  
  13. I use the following to calculate 'day names', the leap year part is
  14. taken care of in the 'year/4...' code.
  15.  
  16. One problem (of many), as mentioned by others, the change of 
  17. calendar in various countries/empires at various times. You might
  18. want to test the year for inclusion in a range appropriate to
  19. your application.
  20.  
  21. Note: this_* variables are pulic String's (a string class).
  22.  
  23. // ------------------------------------------------------------
  24. String     Dates::DayNames ( void )     // return this_day_name
  25. {
  26.     int day_index ;
  27.  
  28.     int day     = atoi ( (char *) this_day ) ;
  29.     int month     = atoi ( (char *) this_month ) ;
  30.     int year     = atoi ( (char *) this_year ) ;
  31.  
  32.     String days[] = { "Sun.", "Mon.", "Tues.", "Wed.", "Thurs.", "Fri.", "Sat." } ;
  33.  
  34. // ---- thanks to Tomohiko Sakamoto
  35.  
  36.     int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4} ;
  37.     
  38.     year -= month < 3 ;
  39.  
  40.     day_index = (year + year/4 - year/100 + year/400 + t[month-1] + day) % 7 ;
  41.  
  42. // ---- end thanks
  43.  
  44.     this_day_name = days[day_index] ;
  45.  
  46.     return ( this_day_name ) ;
  47. }
  48. -- 
  49. +--------------------------------------------------------------------+ 
  50. | Barry B. Floyd                   \\\               floydb1@rpi.edu |
  51. | RPI Alum. '84 '87 '88              \\\                             |
  52. +--------------------------------------------------------------------+
  53.